/**
  @file
  @author Stefan Frings
*/

#include "netlistenerrequesthander.h"

#include <QDateTime>
#include <QJsonDocument>
#include <QJsonObject>

NetListenerRequestHander::NetListenerRequestHander(NetClientJob *clientJob, QObject *parent)
    : HttpRequestHandler(parent)
    , m_clientJob(clientJob)
{

}

void NetListenerRequestHander::service(HttpRequest& request, HttpResponse& response)
{
    response.setHeader("Access-Control-Allow-Headers", "content-type");
    response.setHeader("Access-Control-Allow-Methods", "*");
    response.setHeader("Access-Control-Allow-Origin", "*");

    QByteArray path = request.getPath();
    qInfo() << Q_FUNC_INFO << request.getMethod();
    if ("OPTIONS" == request.getMethod()) {
        return;
    }

    if (path.startsWith("/pclogin/getEnvInfo")) {
        qInfo() << Q_FUNC_INFO << request.getBody();

        QJsonObject jsonObj;
        jsonObj.insert("code", "PCL_SUCCESS");
        jsonObj.insert("msg", "Success");
        jsonObj.insert("data", "true");

        QJsonDocument jsonDoc(jsonObj);
        response.write(jsonDoc.toJson());
    } else if (path.startsWith("/pclogin/sso/getIDToken")) {
        qInfo() << Q_FUNC_INFO << request.getBody();

        QJsonObject jsonObj;
        jsonObj.insert("code", "PCL_SUCCESS");
        jsonObj.insert("msg", "qqq");
        jsonObj.insert("requestID", "PCL20220411000141375511ujshhg");
        jsonObj.insert("idToken", m_clientJob->getIdTocken());
        qInfo() << Q_FUNC_INFO << "idToken" << m_clientJob->getIdTocken();

        QJsonDocument jsonDoc(jsonObj);
        response.write(jsonDoc.toJson());
    }
}
